<?php
namespace Home\Controller;
/**
 * 用户个人信息
 * @author Administrator
 *
 */
class UserController extends HomeBaseController
{
    public function index(){
        $database_user = D('user');
        $condition_user['id'] = session('uid');
        $user = $database_user->field(true)->where($condition_user)->find();
        $this->assign('user', $user);
        $this->display('user/index');
    }
    
    /**
     * 短信验证码发送
     */
    public function sendMobileMes(){
        /**
         * 校验图片验证码
         * */
        $regverify=I('imgcode',1);
        $imgCodeId=I('imgCodeId',1);
        $verify = new \Think\Verify();
        if(!$verify->check($regverify, $imgCodeId)) {
            $this->ajaxReturn(array('code'=>'error','info' => '验证码错误或已过期！'));//0错误，1成功
            exit;
        };
        $user = D('User')->getUserByMobile(I('mobile',''));
        if($user!=null){
            $result =  array('code'=>'error','info' => '此手机号码已被绑定，请核实！');
            $this->ajaxReturn($result,'JSON');
            exit();
        }
        $resp = D('Mess','Event')->sendMobileMes(I('mobile',''));
        $this->ajaxReturn($resp,'JSON');
        
    }
    
    /**
     * 邮箱验证码发送
     */
    public function sendEmailMes(){
        /**
         * 校验图片验证码
         * */
        $regverify=I('imgcode',1);
        $imgCodeId=I('imgCodeId',1);
        $verify = new \Think\Verify();
        if(!$verify->check($regverify, $imgCodeId)) {
            $this->ajaxReturn(array('code'=>'error','info' => '验证码错误或已过期！'));//0错误，1成功
            exit;
        };
        $user = D('User')->getUserByEmail(I('email',''));
        if($user!=null){
            $result = array('code'=>'error','info' => '此邮箱已被绑定，请核实！');
            $this->ajaxReturn($result,'JSON');
            exit();
        };
        $resp = D('Mess','Event')->sendEmailMes(I('email',''));
        $this->ajaxReturn($resp,'JSON');
    }
    
    
    public function saveProfile(){
        $database_user = D('user');
        $condition['id'] = session('uid');
        $resp["status"]=0;
        $resp["msg"]="操作失败！";        
        if(I('post.username','') != ''){
            $data['username'] = I('post.username','');
        };
        if(I('post.sex','') != ''){
            $data['sex'] = I('post.sex','');
        };
        if(I('post.idcard','') != ''){
            $condition_idcard['idcard'] = I('post.idcard','');
            $count = $database_user->field(true)->where($condition_idcard)->count();
            if($count >0 ){
                $resp["msg"]="身份证号已注册，请更换身份证号！！";
                $this->ajaxReturn($resp,'JSON');
            }else{
                $data['idcard'] = I('post.idcard','');
            };
        };
        if(I('post.mobile','') != ''){
            $result = D('Mess','Event')->checkCode(I('codeMobile',''));
            if($result["status"] == 0){
                $this->ajaxReturn($result,'JSON');
                exit();
            }else{
                session('registerCode',null);
                $data['mobile'] = I('post.mobile','');
            };
        };
        if(I('post.email','') != ''){
            $result = D('Mess','Event')->checkCode(I('codeEmail',''));
            if($result["status"] == 0){
                $this->ajaxReturn($resp,'JSON');
                exit();
            }else{
                session('registerCode',null);
                $data['email'] = I('post.email','');
            }
        };
        $result = $database_user->where($condition)->save($data);
        if($result){
            $resp["status"]=1;
            $resp["msg"]="操作成功！";
        };
        $this->ajaxReturn($resp,'JSON');
    }
    
    /**
     * 跳转修改密码页
     */
    public function pwd(){
        $cond['id'] = session('uid');
        $user = D('user')->field(true)->where($cond)->find();
        $this->assign('user', $user);
        $this->display("user/pwd");
    }
    
    
    
    /**
     * 修改登录密码
     */
    public function updatePasswd(){
        $resp['status']=1;
        $resp['msg']="修改成功";
        //1 校验短信验证码
        $result = D('Mess','Event')->checkCode(I('post.code',''));
        if($result["status"] == 0){
            $this->ajaxReturn($result,'JSON');
            exit();
        }else{
            session('registerCode',null);
        };
        //2 修改密码
        if(I('post.changeType','') == "loginpasswd"){
            $user['loginpasswd'] = md5(I('post.pass',''));
            M('user')->where(array('id'=> session('uid')))->save($user);
        };
        if(I('post.changeType','') == "transpasswd"){
            $user['transpasswd'] = md5(I('post.pass',''));
            M('user')->where(array('id'=> session('uid')))->save($user);
        }
        $this->ajaxReturn($resp,'JSON');
    }
    
    /**
     * 修改交易验证方式
     */
    public function updateTransType(){
        $resp['status']=1;
        $resp['msg']="修改成功！";
        $user['transveriftype'] = I('post.tvt','');
        M('user')->where(array('id'=> session('uid')))->save($user);
        $this->ajaxReturn($resp,'JSON');
    }
    
    
    /**
     * 跳转分享
     */
    public function share(){
        $cond['id'] = session('uid');
        $user = D('user')->field(true)->where($cond)->find();
        $count = D('RecommedIncome')->getCount(array('uid'=>session('uid')));
        
        $list = D('RecommedIncome')->getListByTime(session('uid'),I('post.queryTime',1));
        $countList = D('RecommedIncome')->getCountByTime(session('uid'),I('post.queryTime',1));
        
        
        $this->assign('user', $user);
        $this->assign('count', $count);
        
        $this->assign('list', $list);
        $this->assign('countList', $countList);
        
        $this->assign('queryTime', I('post.queryTime',1));
        
        $this->display("user/share");
    }
    
    /**
     * 获得二维码
     */
    public function qrcode(){
        $cond['id'] = session('uid');
        $user = D('user')->field(true)->where($cond)->find();
        vendor("phpqrcode.phpqrcode");
        $domain_name = $this->domain_name;
        $app_path = $this->app_path;
        $data=$domain_name.$app_path.U('register/index')."?pusher=".$user["invit_code"];
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 4;
        // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false
        //$path = "images/";
        // 生成的文件名
        //$fileName = $path.$size.'.png';
        \QRcode::png($data, false, $level, $size);
    }
}